/**
 * 制作:  叶风
 * Qq:    355007778
 * Email: ye.fn@163.com 
 * 
 */


setPoDIV = function(o, opt){
	opt = $.extend({
				t: 0,
				l: 0,
				w: 220
			},opt);
		
	var O = $(o), Obj={};
	O.css({position: 'absolute', padding: '12px', backgroundColor: '#eee',  border:'1px #f3f3f3 solid', width: opt.w});
	$('<a class="close_bnt">&nbsp;</a>').appendTo(O).click(function(){Obj.close();});
	
	var ow = O.outerWidth(), oh = O.outerHeight(), pl = $(window).width() - ow , pt=$(window).height()- oh;
	Obj.show = function(op){
		var po = $(op).offset(), l=po.left + opt.l , t =po.top + opt.t ;
		if (l > pl) l = po.left -ow;
		if (t > pt) t = po.top -oh;
		O.css({left:l, top: t}).show();
	};
	Obj.close = function(){
		O.hide();
	};
	
	return Obj;
}


/**
 * 智能浮动DIV
 * @param {type} o
 * @param {type} pv value
 * @param {type} p  up or bottom
 */ 
function fixDIV(o, pv, p){
	o = $(o);
    pv = pv ? (pv) :0;
	if (p=='bottom')pv = $(window).height()- o.height() - pv;
    
	var of=o.offset(), scrollH = Math.max(0, of.top-pv),  old_style= o.attr('style')||'';
	var	O={ isFix: false,
			set:function(){
					var sh = $(document).scrollTop();
                    if (sh>scrollH){
                        if (!this.isFix){
                            o.css({position:'fixed',top:pv, left:of.left});
                            this.isFix = true;
                        }
					}else if(this.isFix){
						o.attr('style', old_style);
						this.isFix = false;
					} 
					return this;
				},
			un:	function(){
					$(window).unbind('scroll', this.set);
					o.attr('style', old_style);
				}
		 };
	$(window).bind('scroll', O.set);
	return O;
}

/**
 * 滚动
 */
function scrollPic(obj, opt){
	opt = $.extend({
				speed:3,
				sp: 200,
				maxvc: 12
			},opt);
	obj = $(obj);
	if(obj.length>1){
		obj.each(function(){scrollPic(this, opt)});
		return;
	}
	
	opt.speed *=1000;
	var	sdiv = $('.gd-div', obj), maxv = -( sdiv.width() -  $('.gd-box', obj).width()- opt.maxvc );
	function marqueeL(){
		var v;;
		if (sdiv.position().left <= maxv){
			v =0;
		}else{
			v = sdiv.position().left - opt.sp;
			
			if (v <= maxv)
				v = maxv;
			
		}
		sdiv.animate({left: v});
	}
	function marqueeR(){
		var v;;
		if (sdiv.position().left >=0){
			v = maxv;
		}else{
			v = sdiv.position().left + opt.sp;
			if (v>0)
				v=0;
		}
		sdiv.animate({left: v});
	}
	
	var tg;
	obj.hover(function() {clearInterval(tg);}, function(){start();});
	function start(){ tg = setInterval(marqueeL, opt.speed);}
	start();
	
	//hand
	$('.gd-handL', obj).click(function(){if(sdiv.position().left<0) marqueeR();}),
	$('.gd-handR', obj).click(function(){if(sdiv.position().left>maxv) marqueeL();});
}

// 幻灯播放
function slidePic(obj, opt){
	opt = $.extend({pagesClass:'slide-pagesCont',
            		speed:3,
					cur:0,
                    loop:true,
					around: true,
					prevClass:'slide-prev',
					nextClass:'slide-next'
			},opt);
	opt.speed *=1000;
	var O=$(obj), pic = O.children(), i, str;
	O = O.parent();
	if (pic.length<2)return;
	pic.hide();
	
	// pages
	str = '';
	for(i=0;i<pic.length;)
		str += '<a>'+ ++i + '</a>';
	var pagesO = $('<div class="'+ opt.pagesClass +'">' +str+ '</div>').appendTo(O).children();
	pagesO.bind('mouseover',function(){
		opt.cur=$(this).index();
		marquee();
	});
	// prev, next
	if(opt.around){
		var _a =$('<a class="'+ opt.prevClass +'"></a>').click(function(){opt.cur--;marquee();})
		 	    .add($('<a class="'+ opt.nextClass +'"></a>').click(function(){opt.cur++;marquee();}))
			    .appendTo(O).fadeTo(0, 0.6).hide();
		O.hover(function(){_a.show();},  function(){_a.hide()});
	}
	//
	function marquee(){
		if (opt.cur>pic.length-1){
			if (!opt.loop)return;
			opt.cur = 0;
		}
		else if (opt.cur<0){
			if (!opt.loop)return;
			opt.cur = pic.length-1;
		}
		
		var h = pic.filter('.active').removeClass('active'),
			s = pic.eq(opt.cur).addClass('active');
		
		switch(opt.playType){
			case 'down':
				if (opt.cur>h.index()){
					s.slideDown(600,function(){h.hide();});
				}else{
					s.show(0,function(){h.slideUp(600);});	
				}
				break;
			default:
				h.fadeOut(800);
				s.fadeIn(800);
		}
		
		pagesO.filter('.active').removeClass('active');
		pagesO.eq(opt.cur).addClass('active');	
	}
	
    // autoPlay
	var tg;
    function autoP(){tg =setInterval(function(){opt.cur++;marquee();}, opt.speed);}
	O.hover(function(){clearInterval(tg);},
            function(){autoP();});
    
	marquee();
	autoP();
}

function mdtf(l,t,f)
{
	l.live('mousedown', function(e)
	{
			e.stopPropagation();
			var o = $(this);
			var nore = function(){return false;};
			
			var isadd = window.setTimeout(function(){o.bind('click',nore);f(o);},t);
			
			var x= e.screenX, y= e.screenY;
			
			var noadd = function(e){
				var _x=e.screenX-x,_y=e.screenY-y;
				if( _x>3 ||_x<-3  || _y>3 || _y<-3 )
					window.clearTimeout(isadd);
			};
			
			var re = function(){
					window.clearTimeout(isadd);
					o.unbind("mousemove",noadd);
					window.setTimeout(function(){
						o.unbind('mouseup',re);
						o.unbind('click',nore);
					}, 600);
				}
						
			o.bind("mousemove",noadd);
			o.bind('mouseup',re);
	});
}


function q_edit(obj, fun)
{
    obj = $(obj);
    var  inp = $('<input type="text" size="8" />').appendTo('body').focus();
    inp.bind('keydown',function(e){
        if (e.keyCode == 27){
            inp.blur();
        }else if (e.keyCode == 13){
            var v=inp.val();
            obj.html(v);
            fun(v);
            inp.blur();
        }
     })
    .bind('blur',function(){
         inp.remove();
    })
    .offset(obj.offset());
}


/**
 * 卡片切换
 * @param {type} t 按钮
 * @param {type} c 卡片
 * @param string id
 */
function tabs(obj, id, ishover)
{
	if (!obj) obj='.tabB';
	if ($(obj).length>1){
		$(obj).each(function(){tabs(this, id, ishover)});
		return;
	}
	var hti = ishover ? 'click mouseenter' : 'click';
	var t = $('.tab-t', obj);
    var c = $('.tab-c', obj);
	t.each(function(index) {
        if ($(this).find('a').length<1){
			$(this).wrapInner('<a href="#tab-'+ index +'"></a>');
		}
		
    })
	.bind(hti, function(){
		$(this).addClass('active').siblings('.active').removeClass('active');

		c.hide().eq($(this).index()).show();	
	});
	
	if (typeof id=='undefined'){
		var url=window.location.href,index=url.indexOf('#tab-');
		if(index>0){
			id = url.substr(index +5);
			if (id){
				var r = /^\+?[1-9][0-9]*$/;
				if( r.test(id)){
					t.eq(id).click();
					return;
				}
			}
		}
		t.eq(0).click();
	}else
		$('#' + id).click();
}


/**
 * 文件尺寸
 *
 */
function format_fileSize(size)
{
	size = parseInt(size);
	if(size>1000000)
		return Math.ceil(size /1024 /1024) +' MB';
  	else 
		return Math.ceil(size /1024)+' KB';
}


/**
 * 目录树
 * @param {type} o
 * @param {type} url
 * @param {type} htmlF
 */
function dirTree_li(o, url, htmlF)
{
	var opt = {refresh:true,defVal:''};
	if (typeof htmlF=='object')
		opt = mergeConf(opt, htmlF);	
    else
		opt.htmlF = htmlF;
	
    //点击
	var obj = $(o);
	$('p', obj).live('click', function(){
        $('p.active', obj).removeClass('active');
        $(this).addClass('active').find('.has:not(.expand)').click();
	});
        
    //刷新
    if(opt.refresh) $('p .icon-16-reset', obj).live('click',  function(){
        var a=$(this).parent();
        a.next('.subTree').remove();
        a.find('.hand').addClass('has').click();
	});
        
	// view sub
    var bhtml = opt.htmlF ? opt.htmlF : function(o, r){
        if (typeof(r)=='string')
          o.append(r).slideDown(100);
        else{
          if(!checAjaxRe(r)) return;
          var i ,res =r.res,html = '';
          for(i in res){
            var d = res[i];
            html += '<p class="sl-i" i_ID="'+ d.id +'">';
            if (opt.refresh) html += '<span class="fl-r noVis icon-16-reset button_16"></span>';
            html += '<b class="hand'+ (d.subNum>0?' has': '') +'"></b><span class="title sl-h'+ (d.ico?' icon-16 icon-16-'+ htmlEncode_q(d.ico):'') +'">'+  htmlEncode(d.name) +'</span>';
            if (d.iNum) html += '<span class="num">('+d.iNum+')</span>';

            html += '</p>';
          }
          o.append(html).slideDown(100);
        }
	};
    
    // ajax
	$(".has", obj).live('click', function(e){
        var o = $(this),o_p = o.parent();
        e.stopPropagation();
        if(o_p.next('.subTree').length){	
            o_p.next('.subTree').slideToggle();
            o.toggleClass('expand');
        }else{
            o.addClass('expand');
            var a = setLoading(o[0]);
            $.get(url + encodeURIComponent(o_p.attr('i_ID')), '', function(r){
                a.restore();
				var so = $('<div class="subTree none"></div>').insertAfter(o_p);
                bhtml(so, r);
				if (so.html()==''){
					o.removeClass('has');
					so.remove();
				}
            });		 	
        }
	});

	if (obj.html()=='' ||  opt.defVal)$.get(url + opt.defVal, '', function(r){bhtml(obj, r);});
}